package org.springframework.prospring.ticket.service.impl;

import org.springframework.prospring.ticket.service.CityManager;
import org.springframework.prospring.ticket.service.ProvinceManager;
import org.springframework.prospring.ticket.dao.CityDAO;
import org.springframework.prospring.ticket.dao.ProvinceDAO;
import org.springframework.prospring.ticket.domain.location.City;
import org.springframework.prospring.ticket.domain.location.Province;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Collection;
import java.util.Iterator;
import java.util.ArrayList;

public class ProvinceManagerImpl implements ProvinceManager {
    private static Log logger = LogFactory.getLog(ProvinceManagerImpl.class);
    private ProvinceDAO dao;

    public ProvinceManagerImpl() {
        super();
        if (logger.isDebugEnabled()) {
            logger.debug("in ProvinceManagerImpl Constructor");
        }
    }





    public void setProvinceDAO(ProvinceDAO dao) {
        if (logger.isDebugEnabled()) {
            logger.debug("setting ProvinceDAO: " + dao);
        }

        this.dao = dao;
    }

    public Province findProvince(Long provinceId) {

        if (logger.isDebugEnabled()) {
            logger.debug("finding Province id: "+provinceId);
        }
        Province province = dao.findProvince(provinceId);

        if (province == null) {
            logger.warn("ProvinceId '" + provinceId + "' not found in database.");
        }

        return province;
    }

    public Collection <City> findCitiesByProvince(Long provinceId){
        if (logger.isDebugEnabled()) {
            logger.debug("finding Cities for the Province id: "+provinceId);
        }
        Collection<City> cities= dao.findCitiesByProvince(provinceId);

        Collection citiesForView = new ArrayList();
        if (cities == null) {
            logger.warn("Not Found Cities for ProvinceId '" + provinceId + "',in database.");
        } else {
            for (City city: cities){
                City cityForView = new City();
                cityForView = city;
                citiesForView.add(cityForView);
            }
        }


        return citiesForView;
    }


    public Collection<Province> findAllProvinces(){
        if (logger.isDebugEnabled()) {
            logger.debug("finding all Provinces ");
        }

        Collection<Province> provinces = dao.findAllProvinces();

        if (provinces == null) {
            logger.warn("Not found any Provinces in database.");
        }

        return provinces;
    }
}
